package com.easepal7506a.project.widget;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import androidx.constraintlayout.widget.ConstraintLayout;

import com.easepal7506a.project.Constant.CommmandNum;
import com.easepal7506a.project.R;
import com.easepal7506a.project.interfaces.IProMasaChoose;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;

/**
 *特色Spa
 * Created by FHB on 2018/3/26.
 */

public class LayoutSpa_7506 extends ConstraintLayout implements View.OnClickListener{
    private ImageView mIvJjhh;//肩颈呵护
    private ImageView mIvJzfs;//脊柱放松
    private ImageView mIvYtyh;//腰臀养护
    private ImageView mIvXtjz;//纤腿健足

    private List<ImageView> mViewList = new ArrayList<>();
    private IProMasaChoose mAction;
    private MyHandler myHandler;
    public LayoutSpa_7506(Context context) {
        super(context);
    }

    public LayoutSpa_7506(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    public void setAction(IProMasaChoose action){
        mAction = action;
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        myHandler = new MyHandler(this);
        mIvJjhh =  findViewById(R.id.zy_masa_iv_jjhh);
        mIvJzfs =  findViewById(R.id.zy_masa_iv_jzfs);
        mIvYtyh =  findViewById(R.id.zy_masa_iv_ytyh);
        mIvXtjz =  findViewById(R.id.zy_masa_iv_xtjz);
        mViewList.add(mIvJjhh);
        mViewList.add(mIvJzfs);
        mViewList.add(mIvYtyh);
        mViewList.add(mIvXtjz);
        mIvJjhh.setOnClickListener(this);
        mIvJzfs.setOnClickListener(this);
        mIvYtyh.setOnClickListener(this);
        mIvXtjz.setOnClickListener(this);
    }

    public void setSelect(int position) {
        myHandler.obtainMessage(100,position).sendToTarget();
    }
    private void makeChoose(int position){
        if (position == -1) {
            for (int i = 0; i < mViewList.size(); i++) {
                mViewList.get(i).setSelected(false);
            }
        } else {
            for (int i = 0; i < mViewList.size(); i++) {
                if (i == position) {
                    Log.e("getDataPer2","select"+position);
                    mViewList.get(i).setSelected(true);
                } else {

                    mViewList.get(i).setSelected(false);
                }

            }
        }
    }
    private  class MyHandler extends Handler {
        WeakReference<LayoutSpa_7506> mMainActivityWeakReference;

        MyHandler(LayoutSpa_7506 layoutOne) {
            mMainActivityWeakReference = new WeakReference<>(layoutOne);
        }

        @Override
        public void handleMessage(Message msg) {
            final LayoutSpa_7506 mLayoutTwo= mMainActivityWeakReference.get();
            int position = (int)msg.obj;
            Log.e("getDataPer","select"+position);
            if (mLayoutTwo != null) {

                makeChoose(position);
            }
        }
    }


    @Override
    public void onClick(View v) {
        int i = v.getId();
        if (i == R.id.zy_masa_iv_jjhh) {
            mAction.chooseMassage("肩颈呵护", CommmandNum.JIANJHH);

        } else if (i == R.id.zy_masa_iv_jzfs) {
            mAction.chooseMassage("脊柱放松", CommmandNum.JIZFS);

        } else if (i == R.id.zy_masa_iv_ytyh) {
            mAction.chooseMassage("腰臀养护", CommmandNum.YAOTYH);

        }
        else if (i == R.id.zy_masa_iv_xtjz) {
            mAction.chooseMassage("纤腿健足", CommmandNum.XIANTJZ);

        }

    }
}
